use strict;
use warnings;

main();

sub help {
    print "Usage:\n";
    print "Groupsize WORK_FOLDER -r i to generate experiment report for robot i\n";
    print "Groupsize WORK_FOLDER -a start stop to generate experiment report for between robots (start, stop)\n";

    exit(1);
}

sub main {
    help() if not defined $ARGV[0] or not defined $ARGV[1];
    chdir $ARGV[0] . "/data"
        or die "Can't go into $ARGV[0]/data\n";
    
    if($ARGV[1] eq "-r") {
        help() if not defined $ARGV[2];
        GenerateRobotData($ARGV[2]);
    }
    elsif($ARGV[1] eq "-a") {
        help() if not defined $ARGV[2];
        help() if not defined $ARGV[3];
        GenerateAllData($ARGV[2], $ARGV[3]);
    }
    else {
        help();
    }
}

sub GenerateRobotData {
    my $rid = shift;

    print "Processing simulation data from robot " . $rid . "\n";
    open FILE, "Robot_$rid.dat";
    my ( @time, @size );
    foreach(<FILE>) {
        if(/\[Time (\d+)\].*\[GroupSize (\d+)\].*/) {
            push @time, $1;
            push @size, $2;
        }
    }
    close FILE;

    print "Gernerating temp data file for robot " . $rid . "\n";
    open TMP_FILE,">tmp.dat";
    print TMP_FILE $time[$_] . ' ' . $size[$_] . "\n"
        foreach (0 .. scalar(@time) - 1);
    close TMP_FILE;

    print "Drawing image for robot " . $rid . "\n";
    system("gnuplot -e \'plot \"tmp.dat\" with line;pause -1\'");
    last;
}

sub GenerateAllData {
    my ( $startId, $endId ) = ( @_ );
    my ( @time, @avgSize, @maxSize, @minSize );

    print "Processing simulation data for robots($startId, $endId)\n";
    foreach my $rid($startId .. $endId) {
        open FILE, "Robot_$rid.dat";
        my $line = 0;
        foreach(<FILE>) {
            if(/\[Time (\d+)\].*\[GroupSize (\d+)\].*/) {
                $time[$line] = $1 / 1000000 if not defined $time[$line];
                $avgSize[$line] = defined $avgSize[$line] ? $avgSize[$line] + $2 / ($endId - $startId)
                    : $2 / ($endId - $startId);
                $maxSize[$line] = $2 if not defined $maxSize[$line] or $maxSize[$line] < $2;
                $minSize[$line] = $2 if not defined $minSize[$line] or $minSize[$line] > $2;
                $line++;
            }
        }
        close FILE;
    }

    print "Gernerating temp data file for robots\n";
    open TMP_FILE,">tmp.dat";
    print TMP_FILE $time[$_] . ' ' . $avgSize[$_] . ' ' . $maxSize[$_] . ' ' . $minSize[$_] . "\n"
        foreach (0 .. scalar(@time) - 1);
    close TMP_FILE;

    print "Drawing image for all robots\n";
    my $drawCmd = "gnuplot -e \'"
        . "set title \"Group Size Estimate Result\";"
        . "set xlabel \"Time(s)\";"
        . "set ylabel \"Group Size\";"
        . "set grid;"
        . "plot \"tmp.dat\" title \"Pheromone Based\" with line lt rgb \"blue\";"
        . "pause -1"
        . "\'";
    #system("gnuplot -e \'plot \"tmp.dat\" with line, \"tmp.dat\" using 1:3 with line, \"tmp.dat\" using 1:4 with line;pause -1\'");
    system($drawCmd);
}
